package com.knife.bbs.business;

import java.util.ArrayList;
import java.util.List;

import com.knife.bbs.object.BBSDoc;

public class BBSDocService extends DAOSupportService {
	private static BBSDocService singleton=new BBSDocService();
	
	public static BBSDocService getInstance()
	{
		return singleton;
	}
	
	public List<BBSDoc> getDocReply(BBSDoc doc) {
		return queryBBSDoc("parentId='"+doc.getCid()+"'  and status>=0 order by inputTime");
	}
	
	public Number getDocReplyNum(BBSDoc doc){
		Number num=(Number)this.dao.uniqueResult("select count(*) from BBSDoc where parentId='"+doc.getCid()+"' and status>=0");
		return  num;
	}
	
	public  BBSDoc getBBSDoc(String cid){
		com.knife.bbs.domain.BBSDoc doc=(com.knife.bbs.domain.BBSDoc)this.dao.get(com.knife.bbs.domain.BBSDoc.class,cid);
		if(doc!=null){
			return new BBSDoc(doc);
		}else{
			return null;
		}
	}
	
	public  List<BBSDoc> queryBBSDoc(String scope){
		return changeListType(this.dao.query(com.knife.bbs.domain.BBSDoc.class,scope));
	}
	
	public BBSDoc getLastReplyDoc(BBSDoc doc){		
		List<BBSDoc> list=changeListType(this.dao.query(com.knife.bbs.domain.BBSDoc.class,"parentId='"+doc.getCid()+"' and status>=0 order by inputTime desc",null,0,1));
		BBSDoc reply=null;
		if(list!=null && list.size()>0)reply=(BBSDoc)list.get(0);
		if(reply==null)
		{
			reply=new BBSDoc();
			reply.setInputuser("---");
			reply.setInputtime(doc.getInputtime());
		}
		return reply;
	}
	
	public List<BBSDoc> changeListType(List<Object> objs){
		List<BBSDoc> results = new ArrayList<BBSDoc>();
		if(objs!=null){
			if(objs.size()>0){
				for (Object newobj : objs) {
					com.knife.bbs.domain.BBSDoc robj = (com.knife.bbs.domain.BBSDoc) newobj;
					BBSDoc tobj = new BBSDoc(robj);
					results.add(tobj);
				}
			}
		}
		return results;
	}
}
